package com.youi.service.impl;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jaxen.function.IdFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.GoodsimgsDao;
import com.youi.entity.Goods;
import com.youi.entity.Goodsimgs;
import com.youi.model.ImgsObj;
import com.youi.service.GoodsService;
import com.youi.service.GoodsimgsService;

@Service
public class GoodsimgsServiceImpl extends BaseServiceImpl<Goodsimgs> implements GoodsimgsService{
		
		private GoodsimgsDao goodsimgsDao;
		@Autowired
		private GoodsService goodsService;
		
		@Autowired
		public void setfaultRecordDao(GoodsimgsDao goodsimgsDao){
			this.goodsimgsDao=goodsimgsDao;
			super.setBaseDAO(goodsimgsDao);
		}

		@Override
		public void addOrGoodsImg(Long goodsId,Goods goods,Integer type,String imgs) {
			// TODO Auto-generated method stub
			if (goodsId!=null) {
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				params.put("goodsId", goodsId);
				params.put(Common.TYPE, type);
				Goodsimgs goodsimgs=goodsimgsDao.getObject(params);
				if (goodsimgs!=null) {
					goodsimgsDao.delete(goodsimgs);
				}
				if (imgs!=null&&imgs.length()>0) {
					goodsimgsDao.saveOrUpdate(new Goodsimgs(goodsId, type, imgs));
				}				
			}else {
				if (imgs!=null&&imgs.length()>0) {
					goodsService.saveOrUpdate(goods);
					goodsimgsDao.saveOrUpdate(new Goodsimgs(goods.getId(), type, imgs));
				}
			}
			
		}

		@Override
		public void addOrGoodsImg(Long goodsId, Goods goods, Integer type, List<ImgsObj> imgsObjlIList) {
			// TODO Auto-generated method stub
			if (goodsId!=null) {
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				params.put("goodsId", goodsId);
				params.put(Common.TYPE, type);
				List<Goodsimgs> goodsimgsList=goodsimgsDao.getObjectList(params);
				if (goodsimgsList!=null&&goodsimgsList.size()>0) {
					goodsimgsDao.deleteAll(goodsimgsList);
				}
				if (imgsObjlIList!=null&&imgsObjlIList.size()>0) {
					for (ImgsObj imgsObj : imgsObjlIList) {
						if (imgsObj.getImg()!=null&&imgsObj.getImg().length()>0) {							
							goodsimgsDao.saveOrUpdate(new Goodsimgs(goods.getId(), type, imgsObj.getImg()));
						}
					}
				}					
			}else {
				if (imgsObjlIList!=null&&imgsObjlIList.size()>0) {
					for (ImgsObj imgsObj : imgsObjlIList) {
						if (imgsObj.getImg()!=null&&imgsObj.getImg().length()>0) {
							goodsService.saveOrUpdate(goods);
							goodsimgsDao.saveOrUpdate(new Goodsimgs(goods.getId(), type, imgsObj.getImg()));
						}						
					}
				}					
			}
		}
}